package com.example.game.sdk.framework

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.KeyEvent
import com.example.game.sdk.framework.contract.ActivityLifecycleContract
import com.example.game.sdk.framework.contract.AppLifecycleContract
import com.example.game.sdk.framework.contract.InitializeContract
import com.example.game.sdk.framework.plugin.Constants
import com.example.game.sdk.framework.plugin.DispatcherTemplate
import com.example.game.sdk.framework.plugin.channel.ChannelDispatcher
import com.example.game.sdk.framework.plugin.privacy.PrivacyDispatcher
import org.json.JSONObject

internal object GameSDKInternal : AppLifecycleContract, ActivityLifecycleContract, InitializeContract {

    private val dispatcherMap = mapOf<String,DispatcherTemplate>(
        Constants.DISPATCHER_CHANNEL_NAME to ChannelDispatcher(),
        Constants.DISPATCHER_PRIVACY_NAME to PrivacyDispatcher()
    )

    /**
     *  获取指定插件
     * @param name 插件名称
     */
    fun getDispatcher(name: String): DispatcherTemplate {
         return dispatcherMap[name]!!
    }

    /**
     * 遍历所有插件，调用指定方法
     */
    private fun forEachDispatcher(action: (DispatcherTemplate) -> Unit) {
        dispatcherMap.forEach { item ->
            action(item.value)
        }
    }

    override fun attachBaseContext(context: Context) {
        forEachDispatcher {
            it.attachBaseContext(context)
        }
    }

    override fun onAppCreate(app: Application) {
        forEachDispatcher {
            it.onAppCreate(app)
        }
    }

    /**
     * 执行sdk初始化，根据业务判断初始化成功/失败
     */
    fun init(params: JSONObject, callback: GameApiCallback) {

    }


    /**
     * 通知插件初始化成功
     */
    override fun init(params: JSONObject) {
        forEachDispatcher {
            it.init(params)
        }
    }


    override fun onCreate(activity: Activity, savedInstanceState: Bundle?) {
        forEachDispatcher {
            it.onCreate(activity, savedInstanceState)
        }
    }

    override fun onStart(activity: Activity) {
        forEachDispatcher {
            it.onStart(activity)
        }
    }

    override fun onResume(activity: Activity) {
        forEachDispatcher {
            it.onResume(activity)
        }
    }

    override fun onPause(activity: Activity) {
        forEachDispatcher {
            it.onPause(activity)
        }
    }

    override fun onStop(activity: Activity) {
        forEachDispatcher {
            it.onStop(activity)
        }
    }

    override fun onDestroy(activity: Activity) {
        forEachDispatcher {
            it.onDestroy(activity)
        }
    }

    override fun onRestart(activity: Activity) {
        forEachDispatcher {
            it.onRestart(activity)
        }
    }

    override fun onActivityResult(
        activity: Activity,
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        forEachDispatcher {
            it.onActivityResult(activity, requestCode, resultCode, data)
        }
    }

    override fun onConfigurationChanged(
        activity: Activity,
        newConfig: Configuration
    ) {
        forEachDispatcher {
            it.onConfigurationChanged(activity, newConfig)
        }
    }

    override fun onBackPressed(activity: Activity): Boolean {
        // 遍历所有插件，调用 onBackPressed 方法, 如果有插件返回 true, 则后续插件不调用
//        dispatcherList.forEach { dispatcher ->
//            if (dispatcher.onBackPressed(activity)) {
//                return true
//            }
//        }
//        return false

//        // 所有插件的 onBackPressed 方法都会被调用，记录是否有插件返回 true
        var isIntercepted = false
        dispatcherMap.forEach { item ->
            val result = item.value.onBackPressed(activity)
            isIntercepted = isIntercepted || result
        }
        return isIntercepted
    }

    override fun onKeyDown(
        activity: Activity,
        keyCode: Int,
        event: KeyEvent?
    ): Boolean {
        // 遍历所有插件，调用 onKeyDown 方法, 如果有插件返回 true, 则后续插件不调用
//        dispatcherList.forEach { dispatcher ->
//            if (dispatcher.onKeyDown(activity,keyCode, event)) {
//                return true
//            }
//        }
//        return false

        // 所有插件的 onKeyDown 方法都会被调用，记录是否有插件返回 true
        var isIntercepted = false
        dispatcherMap.forEach { item ->
            val result = item.value.onBackPressed(activity)
            isIntercepted = isIntercepted || result
        }
        return isIntercepted
    }

    override fun onRequestPermissionsResult(
        activity: Activity,
        requestCode: Int,
        permissions: Array<out String?>,
        grantResults: IntArray
    ) {
        forEachDispatcher {
            it.onRequestPermissionsResult(activity, requestCode, permissions, grantResults)
        }
    }




}